﻿using System.Data.EntityClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ZhiHeng.Consume.DataContract;

namespace ZhiHeng.Consume.ServiceLib.DAL
{ 
    public class DBContextFactory
    {
        private static readonly DBContextFactory _instance = new DBContextFactory();

        public OneCardParkEntities GetDB()
        {
            string s = System.Configuration.ConfigurationManager.ConnectionStrings["OneCardParkEntities"].ConnectionString;
            var entityConnStr = GetEntityFrameworkConnectionString(s);
            return new OneCardParkEntities(entityConnStr);
        }

        private string GetEntityFrameworkConnectionString(string clientConnectionString)
        { 
            var entityBuilder = new EntityConnectionStringBuilder();
            entityBuilder.Provider = "System.Data.SqlClient";
            entityBuilder.ProviderConnectionString = clientConnectionString;
            entityBuilder.Metadata = "res://*/OneCard.csdl|res://*/OneCard.ssdl|res://*/OneCard.msl";
            return entityBuilder.ToString();
        }

        public static DBContextFactory Instance
        {
            get
            {
                return _instance;
            }
        }
    }


}
